package com.my.learn;

import com.lmax.disruptor.EventFactory;
import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.YieldingWaitStrategy;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.dsl.ProducerType;

import java.nio.ByteBuffer;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Main {


    public static void main(String[] args){
        //创建可缓存的线程
        ExecutorService executorService= Executors.newCachedThreadPool();
        //创建工厂
        EventFactory<LongEvent> eventEventFactory=new LongEventFactory();
        //创建ringbuffer 大小
        int ringBufferSize=1024*1024;
        //创建Disruptor
        Disruptor<LongEvent> disruptor=new Disruptor<LongEvent>(eventEventFactory,ringBufferSize,executorService, ProducerType.SINGLE,new YieldingWaitStrategy());
        // 连接消费端方法
        disruptor.handleEventsWith(new LongEventHandler());
        //启动
        disruptor.start();
        //创建RingBuff的容器
        RingBuffer<LongEvent> ringBuffer=disruptor.getRingBuffer();

        //创建生产者
        LongEventPrudocer prudocer=new LongEventPrudocer(ringBuffer);

        //制定缓冲区的大小
        ByteBuffer byteBuffer=ByteBuffer.allocate(8);
        for (int i = 0; i < 100; i++) {
            byteBuffer.putLong(0,i);
            prudocer.onData(byteBuffer);
        }

        //关闭disruptor和executorService
        disruptor.shutdown();
        executorService.shutdown();
    }
}
